<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>用户管理</title>
    <!-- CSS 和 JavaScript 引用 -->
    <link rel="stylesheet" href="../../static/css/bootstrap.4.3.1.min.js.css"
          th:href="@{/css/bootstrap.4.3.1.min.js.css}"/>
    <link rel="stylesheet" href="../../static/css/layui.css" th:href="@{/css/layui.css}"/>
    <script th:src="@{/js/jquery-3.4.1.js}"></script>
    <script th:src="@{/js/layui.js}"></script>
    <script th:src="@{/js/modules/layer.js}"></script>
    <style>
        body {
            background-color: #F5F6F9;
            font-family: 'Arial', sans-serif;
        }

        .header {
            background-color: #fff;
            text-align: center;
            padding: 10px 0;
        }

        .header h1 {
            margin: 0;
            color: #333;
            font-size: 24px;
        }

        .container {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }

        .btn {
            margin: 10px 5px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .btn-success {
            background-color: #28a745;
            border-color: #28a745;
        }

        .btn-outline-info {
            border-color: #17a2b8;
            color: #17a2b8;
        }

        .btn-primary {
            background-color: #007bff;
            border-color: #007bff;
        }

        .btn-danger {
            background-color: #dc3545;
            border-color: #dc3545;
        }

        .table {
            margin-top: 20px;
        }

        .table th, .table td {
            vertical-align: middle;
        }

        .table thead th {
            background-color: #f8f9fa;
            color: #495057;
        }

        .table-striped tbody tr:nth-of-type(odd) {
            background-color: rgba(0, 0, 0, .05);
        }

        .table-responsive {
            display: block;
            width: 100%;
            overflow-x: auto;
            -webkit-overflow-scrolling: touch;
        }

        .table {
            width: 100%;
            min-width: 750px; /* 设置表格的最小宽度 */
            max-width: 100%; /* 确保表格不会超过容器宽度 */
            margin-bottom: 1rem;
            background-color: transparent;
        }
        .operation-buttons {
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 10px;
        }

        .btn {
            padding: 0.25rem 0.5rem;
            font-size: 0.875rem;
            line-height: 1.5;
            border-radius: 0.2rem;
        }
    </style>
</head>
<body style="background-color: #F5F6F9">
<!-- 页头部分 -->
<div style="background-color: white">
    <h1 style="text-align: center">公寓水电管理系统</h1>
</div>
<br>
<!-- 主体内容 -->
<div class="container" style="background-color: white">
    <!-- 功能按钮 -->
    <button onclick="get1234()" class="btn btn-success" style="height:50px">新增住户</button>
    <button class="btn btn-outline-info" onclick="outLogon()">退出登录</button>
    <button class="btn btn-outline-info" style="float: right" onclick="message()">消息</button>

    <!-- 表格内容 -->
    <div class="table-responsive">
        <table class="table table-striped table-bordered table-hover text-center" id="yxy123">
            <thead>
            <tr>
                <!-- 表头 -->
                <th>序号</th>
                <th>用户名</th>
                <th>头像</th>
                <th>密码</th>
                <th>门牌号</th>
                <th>总用水量/吨</th>
                <th>总用电量/度</th>
                <th>电价（元/度）</th>
                <th>水价（元/吨）</th>
                <th>总费用（元）</th>
                <th>管理员</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <tr th:each="user111:${user123}">
                <!-- 表格内容，使用Thymeleaf模板引擎动态填充 -->
                <td th:text="${user111.id}"></td>
                <td th:text="${user111.username}"></td>
                <td>
                    <div th:if="${user111.userAvatarUrl} == null">
                        <input type="file" accept="image/*" id="avatarInput" style="display: none;">
                        <button class="btn btn-secondary" onclick="uploadAvatar(this.value)" th:value = "${user111.id}">上传</button>
                    </div>
                    <!-- 如果头像路径不为null，则显示用户头像 -->
                    <div th:if="${user111.userAvatarUrl} != null">
                        <img th:src="@{'/'+${user111.userAvatarUrl}}"  style="max-width: 100px; max-height: 100px;" >
                    </div>
                </td>
                <td th:text="${user111.pwd}"></td>
                <td th:text="${user111.houseNumber}"></td>
                <td th:text="${user111.waterConsumption}"></td>
                <td th:text="${user111.powerConsumption}"></td>
                <td th:text="${user111.electricityPrice}"></td>
                <td th:text="${user111.waterPrice}"></td>
                <td th:text="${user111.totalCost}"></td>
                <td th:text="${user111.state}"></td>
                <td>
                    <!-- 操作按钮 -->
                    <div class="operation-buttons">
                    <button class="btn btn-primary" th:value="${user111.id}" onclick="findWater(this.value)">水电表
                    </button>
                    <button class="btn btn-primary" th:value="${user111.id}" id="yy1" onclick="get5678(this.value)">
                        更改
                    </button>
                    <button class="btn btn-danger" th:value="${user111.id}" id="yy2" onclick="del123(this.value)">删除
                    </button>
                    </div>
                </td>

            </tr>
            </tbody>
        </table>
    </div>
</div>

<script>
    // 函数定义
    function findWater(userId) {
        // 构建目标URL
        const targetUrl = "/root/waterElectricity?id=" + userId;
        window.location.href = targetUrl;
    }

    function get5678(data) {
        // 编辑住户信息
        let baseApi1 = "/root/";
        commonMethod123.layerOpen("修改住户信息", baseApi1 + 'edit?id=' + data);
    }

    function get1234() {
        // 新增住户信息
        let baseApi1 = "/root/";
        commonMethod123.layerOpen("新增住户信息", baseApi1 + 'insert1');
    }

    function del123(data) {
        // 删除住户信息
        let baseApi1 = "/root/";
        commonMethod111.layerOpen("删除住户信息", baseApi1 + 'del?id=' + data);
    }
    function message(){
        let baseApi1 = "/root/";
        commonMethod123.layerOpen("消息列表", baseApi1 + 'show msg?username=root');
    }

    var commonMethod123 = {
        layerOpen: function (title, url) {
            layui.use('layer', function () {
                var layer = layui.layer;
                layer.open({
                    type: 2,
                    maxmin: true,
                    title: title,
                    shade: 0.1,
                    area: ['700px', '600px'],
                    content: url
                });
            });
        }
    }

    var commonMethod111 = {
        layerOpen: function (title, url) {
            layui.use('layer', function () {
                var layer = layui.layer;
                layer.open({
                    type: 2,
                    maxmin: true,
                    title: title,
                    shade: 0.1,
                    area: ['300px', '200px'],
                    content: url
                });
            });
        }
    }

    function outLogon() {
        // 退出登录
        alert("退出登录成功");
        window.open("/logon", "_parent");
    }
    function uploadAvatar(userId) {
        var fileInput = document.getElementById('avatarInput');
        fileInput.click(); // 触发文件输入点击

        fileInput.addEventListener('change', function () {
            var file = fileInput.files[0];
            if (file) {
                var formData = new FormData();
                formData.append('file', file);

                // 如果需要，添加其他数据（例如用户 ID）
                formData.append('userId', userId);

                // 使用 AJAX 将文件发送到服务器
                $.ajax({
                    url: '/Avatar/upload', // 替换为您的服务器端点
                    type: 'POST',
                    data: formData,
                    contentType: false,
                    processData: false,
                    success: function (response) {
                        // 处理成功（例如，在表格中更新头像 URL）
                        // 在表格中更新头像 URL
                        alert('头像上传成功！');
                    },
                    error: function (error) {
                        // 处理错误
                        alert('上传头像时出错。');
                    }
                });
            }
        });
    }
</script>
</body>
</html>
